//
//  ViewController.m
//  绘制图片类型的图形上下文(Bitmap context)
//
//  Created by 伍银荣 on 2021/7/2.
//  Copyright © 2021 com.BitmapContext. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
   // [self drawBitmapContextImage];
    [self drawBitmapContextForClipImage];
}

// 绘制图片类型的图形上下文.并且裁剪图片
- (void)drawBitmapContextForClipImage {
    UIImage *image = [UIImage imageNamed:@"热巴"];
    //1.开启图片类型的图形上下文
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径,绘制一个圆形
    CGContextAddArc(ctx, image.size.width * 0.5, image.size.height * 0.5, image.size.width * 0.5, 0, M_PI * 2, 1);
    //裁剪成圆形
    CGContextClip(ctx);
    //把图片画上去(把图片放到上文中,并且进行渲染)
    [image drawAtPoint:CGPointZero];
    //获取图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片类型的图形上下文
    UIGraphicsEndImageContext();
    //测试
    self.imageView.image = newImage;
    
    //保存到系统相册
    UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), @"1234");
}

// 指定回调方法,保存到相册的回调
- (void)image:(UIImage *)image  didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if(!error){
        NSLog(@"save success----%@",contextInfo);
    }else{
        NSLog(@"save failed");
    }
}


//绘制图片类型的图形上下文(Bitmap context),并将image保存到沙盒
- (void)drawBitmapContextImage {
    NSLog(@"touchesBegan---在imageView上绘制图片");
    //绘制图片类型的图形上下文(Bitmap context)
    //1.开启图形类型的图形上下文
    // UIGraphicsBeginImageContext(CGSizeMake(300, 300));
    //opaque :不透明,设置为NO,表示是透明的,否则设置为YES的话就是黑色的
    //scale: 缩放程度,直接写零就好了,他可以匹配各种尺寸的图片,保证绘制的图形不失真.
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 300), NO, 0);
    //获取图形上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //拼接路径,同时把路径添加到上下文中
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    CGContextSetLineWidth(ctx, 10);
    [[UIColor redColor] set];
    //渲染
    CGContextStrokePath(ctx);
    //通过图片类型的图形上下文获取图片
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    //关闭图片类型的图形上下文
    UIGraphicsEndImageContext();
    //把获取到的图片放到图片框上
    self.imageView.image = image;
    
    
    NSLog(@"home = %@",NSHomeDirectory());
    //将图片保存到沙盒
    //将image对象转化成NSData类型  Representation:代表
   // NSData *data = UIImagePNGRepresentation(image);
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
    //创建文件
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"xxx.png"];
    //通过data 的writeToFile将image写入沙盒
    [data writeToFile:filePath atomically:YES];
}


@end
